const mysql = require('mysql2/promise');

async function checkTables() {
  try {
    const connection = await mysql.createConnection({
      host: 'localhost',
      port: 3306,
      user: 'root',
      password: '123456',
      database: 'pet'
    });

    console.log('✅ 成功连接到数据库 pet\n');

    // 查询所有表
    const [tables] = await connection.query('SHOW TABLES');
    
    console.log('📋 数据库中的表：');
    console.log('='.repeat(50));
    tables.forEach((row, index) => {
      const tableName = Object.values(row)[0];
      console.log(`${index + 1}. ${tableName}`);
    });
    console.log('='.repeat(50));
    console.log(`\n总共 ${tables.length} 个表\n`);

    // 查看每个表的结构
    for (const row of tables) {
      const tableName = Object.values(row)[0];
      const [columns] = await connection.query(`DESCRIBE ${tableName}`);
      
      console.log(`\n📝 表 "${tableName}" 的结构：`);
      console.log('-'.repeat(80));
      console.log('字段名'.padEnd(25), '类型'.padEnd(20), 'NULL'.padEnd(8), '键'.padEnd(8), '默认值');
      console.log('-'.repeat(80));
      columns.forEach(col => {
        console.log(
          col.Field.padEnd(25),
          col.Type.padEnd(20),
          col.Null.padEnd(8),
          (col.Key || '').padEnd(8),
          col.Default || ''
        );
      });
    }

    await connection.end();
  } catch (error) {
    console.error('❌ 错误:', error.message);
  }
}

checkTables();
